package com.xyc.goods.service.impl;

import com.xyc.global.bean.PageBean;
import com.xyc.global.bean.WxbGood;
import com.xyc.goods.mapper.GoodsMapper;
import com.xyc.goods.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;
    /*@Override
    public List<GoodsTypeBean> queryWxbTypeList() {
        return goodsMapper.queryWxbTypeList();
    }*/

    @Override
    public PageBean queryGoodsInfo(Integer currentPage) {
        List<WxbGood> wxbGoods = goodsMapper.queryGoodsInfo();//得到所有数据
        PageBean pageBean = new PageBean();//创建一个容器

        pageBean.setCurrentPage(currentPage);//当前页是1

        Integer tatolSize = goodsMapper.countPage();//设置总数据
        pageBean.setTatolSize(tatolSize);

        Integer pageSize = 3;
        pageBean.setPageSize(pageSize);//设置每页显示数据

        Integer tatolPage = tatolSize / pageSize == 0 ? (tatolSize / pageSize) : (tatolSize / pageSize + 1);
        pageBean.setTatolPage(tatolPage);//总页数

        int begin = (currentPage - 1) * pageSize;//从哪里查
        List<WxbGood> wxbGoodList = new ArrayList<WxbGood>();//创建一个容器

        int count = begin + pageSize;

        int size = wxbGoods.size();//获取wxbGoods长度
        for (int i = begin; i < count; i++){
            if (i >= size){
                break;
            }
            WxbGood wxbGood = wxbGoods.get(i);
            wxbGoodList.add(wxbGood);
        }
        pageBean.setWxbGoods(wxbGoodList);
        return pageBean;
    }

    /*@Override
    public void updateState(Integer state,String id) throws Exception {
        String[] ids = id.split(",");
        goodsMapper.updateState(state,ids);
    }*/

    @Override
    public Integer countPage() {
        return goodsMapper.countPage();
    }
}
